package com.youngwang.join;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableBiMap;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author youngwang
 *         Date: 2016/8/31
 */
public class JoinerTest {



    public static void main(String[] args) {
        List<String> list = Arrays.asList("W", "a", "n", "g", null, "Y", "a", "n", "g");
        if (!CollectionUtils.isEmpty(list))
            System.out.println(join(list, ""));

        System.out.println(Joiner.on("&").withKeyValueSeparator("=").join(ImmutableBiMap.of("id", "123", "name", "green")));

        Map<String, String> result = Splitter.on("&").withKeyValueSeparator("=").split("id=123&name=green");
        for (Map.Entry<String, String> entry : result.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }

    /**
     * 连接字符串并过滤点null
     *
     * @param stringList 需要连接的字符串列表
     * @param delimiter  连接符
     * @return
     */
    public static String join(Collection stringList, String delimiter) {
        return Joiner.on(delimiter).skipNulls().join(stringList);
    }
}

